'use strict';
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var navSide         = require('page/common/nav-side/index.js');
var _mm             = require('util/mm.js');
var _user           = require('service/user-service.js');
var templateIndex   = require('./index.string');

var page = {
    data: {
           stateId         : _mm.getUrlParam('stateId')    || '',
    },
    init : function(){
        this.onLoad();
    },
    onLoad : function(){
        this.loadList();
		this.bindEvent();
    },
	bindEvent : function(){
	    var _this = this;
	    // 点击提交按钮后的动作
	    $(document).on('click', '.btn-submit', function(){
			var userData=$("#questionForm").serialize();
			userData=userData.replaceAll("=","e");
			userData=userData.replaceAll("&",",");
			//userData="data="+userData;
			var paperName=$("#paperTitle").text();			
			alert(userData);
	        _user.referTestPaper(userData,paperName,function(res){
				window.location.href = './result.html?type=paper&value='+res;
				//alert(res);
		       //	alert("保存成功");		
	       }, function(errMsg){
	           _mm.errorTips(errMsg);
	       });
	                  
	    });
	},
   // 加载list数据
   loadList : function(){
        var   _this       = this;
        var   testHtml    = '';
        var   stateId     = this.data.stateId;
		var   orderDetailHtml = '';
       // 请求接口
       _user.findTestCategory(stateId,function(res){
		   testHtml    = _mm.renderHtml(templateIndex, {
		       list   :  res.list,
			   title  :  res.title
		   });
		   //alert(res.list.length);
		   if(res.list == null){
			   _mm.errorTips(errMsg);
		   }
		   _this.dataFilter(res);
		   orderDetailHtml = _mm.renderHtml(templateIndex, res);
		   $('#questionForm').html(orderDetailHtml);
		   //$('#questionForm').html(testHtml);
       }, function(errMsg){
           _mm.errorTips(errMsg);
       });
   },
   // 数据的适配
   dataFilter : function(data){  	
       data.button   = data.list.length == 18;
   }
    
};
$(function(){
    page.init();
});